﻿using BigEgg.Framework.Foundation;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace FMStudio.Domain.SQLite
{
    [MetadataType(typeof(IItem))]
    public partial class Item : IItem, IDataErrorInfo
    {
        #region Implement IDataErrorInfo Interface
        [NonSerialized]
        private readonly DataErrorInfoSupport dataErrorInfoSupport;

        string IDataErrorInfo.Error { get { return this.dataErrorInfoSupport.Error; } }

        string IDataErrorInfo.this[string memberName] { get { return this.dataErrorInfoSupport[memberName]; } }
        #endregion


        public Item()
        {
            this.dataErrorInfoSupport = new DataErrorInfoSupport(this);

            //  Default value
            this.Standard = string.Empty;
            this.Name = string.Empty;
            
            this.TrademarkReference.AssociationChanged += TrademarkReferenceAssociationChanged;
        }


        #region Implement IItem Interface
        ITrademark IItem.Trademark
        {
            get { return this.Trademark; }
            set { this.Trademark = value as Trademark; }
        }
        #endregion


        #region Private Methods
        private void TrademarkReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("Trademark");
            this.TrademarkID = this.Trademark.ID;
        }
        #endregion
    }
}
